const SwaggerGenerator = require('../utils/swaggerGenerator');
const path = require('path');

// 定义需要添加文档的路由
const routeConfigs = {
  'users.js': [
    {
      method: 'POST',
      path: '/',
      summary: '创建新用户',
      tag: 'Users',
      options: {
        description: '创建一个新的用户账户',
        requestBody: {
          ref: 'User'
        },
        responses: {
          201: {
            description: '用户创建成功',
            schema: 'User'
          },
          400: {
            description: '请求参数错误'
          }
        }
      }
    },
    {
      method: 'PUT',
      path: '/:id',
      summary: '更新用户信息',
      tag: 'Users',
      options: {
        description: '根据用户ID更新用户信息',
        parameters: [
          {
            in: 'path',
            name: 'id',
            type: 'integer',
            required: true,
            description: '用户ID'
          }
        ],
        requestBody: {
          type: 'object',
          properties: {
            name: { type: 'string', description: '用户姓名' },
            email: { type: 'string', description: '用户邮箱' },
            phone: { type: 'string', description: '用户电话' },
            status: { type: 'string', description: '用户状态' }
          }
        },
        responses: {
          200: {
            description: '用户更新成功',
            schema: 'User'
          },
          404: {
            description: '用户不存在'
          }
        }
      }
    },
    {
      method: 'DELETE',
      path: '/:id',
      summary: '删除用户',
      tag: 'Users',
      options: {
        description: '根据用户ID删除用户',
        parameters: [
          {
            in: 'path',
            name: 'id',
            type: 'integer',
            required: true,
            description: '用户ID'
          }
        ],
        responses: {
          200: {
            description: '用户删除成功',
            schema: 'ApiResponse'
          },
          404: {
            description: '用户不存在'
          }
        }
      }
    }
  ],
  'auth.js': [
    {
      method: 'POST',
      path: '/logout',
      summary: '用户登出',
      tag: 'Authentication',
      options: {
        description: '用户登出系统',
        responses: {
          200: {
            description: '登出成功',
            schema: 'ApiResponse'
          }
        }
      }
    }
  ]
};

// 批量添加Swagger文档
async function addSwaggerDocs() {
  console.log('🚀 开始批量添加Swagger文档...');
  
  for (const [fileName, routes] of Object.entries(routeConfigs)) {
    const filePath = path.join(__dirname, '../routes', fileName);
    console.log(`\n📝 处理文件: ${fileName}`);
    
    const success = await SwaggerGenerator.addSwaggerToFile(filePath, routes);
    if (success) {
      console.log(`✅ ${fileName} 处理完成`);
    } else {
      console.log(`⚠️ ${fileName} 处理跳过`);
    }
  }
  
  console.log('\n🎉 批量添加Swagger文档完成！');
  console.log('\n💡 提示: 重启服务器后访问 http://localhost:3000/api-docs 查看更新的文档');
}

// 如果直接运行此脚本
if (require.main === module) {
  addSwaggerDocs().catch(console.error);
}

module.exports = { addSwaggerDocs, routeConfigs };